import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.BevelBorder;

public class MusicGUI {
	
	private DarrellPod myPod;
	private JLabel titleLabel;
	private JList<String> songJList;
	private int currentSongPosition;

	public static void main(String[] args) {
		new MusicGUI();
	}
	
	public MusicGUI() {
		myPod = new DarrellPod();
		currentSongPosition = -1;
		
		Song firstSong = new Song("aa", "aa", 10);
		Song secondSong = new Song("bb", "bb", 20);
		Song thirdSong = new Song("cc", "cc", 20);
		Song a = new Song("cc", "cc", 20);
		Song b = new Song("cc", "cc", 20);
		Song c = new Song("cc", "cc", 20);
		Song d = new Song("cc", "cc", 20);
		Song e = new Song("cc", "cc", 20);
		Song f = new Song("cc", "cc", 20);
		Song g = new Song("cc", "cc", 20);
		Song h = new Song("cc", "cc", 20);
		
		myPod.addSong(firstSong);
		myPod.addSong(secondSong);
		myPod.addSong(thirdSong);
		myPod.addSong(a);
		myPod.addSong(b);
		myPod.addSong(c);
		myPod.addSong(d);
		myPod.addSong(e);
		myPod.addSong(f);
		myPod.addSong(g);
		myPod.addSong(h);

		
		JFrame mainFrame = new JFrame();
		mainFrame.setSize(400, 300);
		mainFrame.setLocationRelativeTo(null);
		mainFrame.setLayout(new FlowLayout());
		
		String [] songTitles = new String [myPod.size()];
		
		for(int i = 0; i < myPod.size(); i++) {
			Song aSong = myPod.getSong(i);
			String title = aSong.getTitle();
			
			songTitles[i] = title;
		}
		
		songJList = new JList<String>(songTitles);
		
		JScrollPane scrollPane = new JScrollPane(songJList);

		
		JPanel buttonPanel = new JPanel();
		buttonPanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
		
		JButton forwardButton = new JButton("Forward");
		forwardButton.setSize(30, 15);
		forwardButton.addActionListener(new ForwardButtonAction());
		
		JButton backButton = new JButton("Back");
		backButton.setSize(30, 15);
		backButton.addActionListener(new BackButtonAction());
		
		buttonPanel.add(forwardButton);
		buttonPanel.add(backButton);
		
		String currentSong = myPod.getCurrentSong().getTitle();
		titleLabel = new JLabel(currentSong);
		
		mainFrame.add(scrollPane);
		mainFrame.add(titleLabel);
		mainFrame.add(buttonPanel);
		
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		mainFrame.setVisible(true);
	}
	
	class ForwardButtonAction implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			myPod.playNextSong();
			currentSongPosition++;
			
			String currentSong = myPod.getCurrentSong().getTitle();
			titleLabel.setText(currentSong);
			
			songJList.setSelectedIndex(currentSongPosition);
		}	
	}
	
	class BackButtonAction implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			myPod.playPreviousSong();
			
			String currentSong = myPod.getCurrentSong().getTitle();
			titleLabel.setText(currentSong);
		}	
	}

}
